package lab03.test07;

import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Test07 {
    public static void main(String[] args) {
        String path = "F:\\test.zip";
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(path);

           Enumeration entries =  zipFile.entries();
            //Returns an enumeration of the ZIP file entries.条目

            ZipEntry zipEntry = null;

           while(entries.hasMoreElements()){

               zipEntry = (ZipEntry ) entries.nextElement();
               System.out.println(zipEntry.getName());


                //创建一个test文件夹
               File testFile  = new File("F:\\test");
               testFile.mkdir();
                //字节流 读文件 写文件
               InputStream fileInputStream = zipFile.getInputStream(zipEntry);

               FileOutputStream fileOutputStream = new FileOutputStream(
                       new File("F:\\test\\" +zipEntry.getName()));

               byte[] data = new byte[1024];
               while((fileInputStream.read(data)) !=  -1){
                   fileOutputStream.write(data);
               }
               fileInputStream.close();
               fileOutputStream.close();
           }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
